package org.jeecgframework.web.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.service.UploadCinemaService;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.pojo.base.ZyUpCmThread;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**   
 * @Title: UploadCinemaServiceImpl
 * @Description: 上传影院信息
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Service("uploadCinemaService")
@Transactional
public class UploadCinemaServiceImpl extends CommonServiceImpl implements
		UploadCinemaService {

	
	/**
	 * @Description 解析EXCEL
	 * @param HttpServletRequest 通过前台请求获得文件流
	 */ 
	@Override
	public List<String> parseExcel(HttpServletRequest req,AjaxJson j) {
		String filePath = req.getParameter("filePath");
		List<String> cinemaNameList = new ArrayList<String>();
		j.setObj(filePath);
		XSSFWorkbook hwk;
		try {
			hwk = new XSSFWorkbook(new FileInputStream(new File(filePath)));
			cinemaNameList = getCinemaNameFromExcel(hwk);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cinemaNameList;
	}

	/**@author xiul
	 * 2016-6-7 下午4:40:07
	 * Description 单取出 影院名称
	 * @return cinemaName List
	 */
	private List<String> getCinemaNameFromExcel(XSSFWorkbook xssfWb) {
		List<String> cinemaNameList = new ArrayList<String>();
		XSSFSheet sheet = xssfWb.getSheetAt(0);//默认只取第一个sheet页
    	try {
        	int cmNameColumNum = 1;
    		int countRow = sheet.getPhysicalNumberOfRows();
    		for (int r = 1;r < countRow;r++) {
    			cinemaNameList.add(ExcelUtil.getCellValue(sheet.getRow(r).getCell(cmNameColumNum)));
			}
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
		return cinemaNameList;
	}
	
	@Override
	public String checkCmNameRepaet(List<String> cmNameList,int begin,int end) {
		System.out.println("beginR:" + begin);
		System.out.println("endR:" + end);
		for(int i = begin;i <= end;i++ ){
			for(int c = i + 1;c < cmNameList.size();c++){
				if(cmNameList.get(i).equals(cmNameList.get(c))){
					return cmNameList.get(i);
				}
			}
		}
		return null;
	}
	
	@Override
	public void readExcel(String filePath) {
		File excelFile = new File(filePath);
		FileInputStream is;
		try {
			is = new FileInputStream(excelFile);
			XSSFWorkbook xssfWb = new XSSFWorkbook(is);
			XSSFSheet sheet = xssfWb.getSheetAt(0);
			int maxUnit = 2000;//每N 条 启动一个线程
    		int countRow = sheet.getPhysicalNumberOfRows();
    		int threadC = countRow / maxUnit;
    		int points = countRow % maxUnit;
    		if(threadC > 0){
    			if(points > 0){
    				threadC = threadC + 1;
    			}
    		}else{
    			threadC = 1;
    		}
    		System.out.println("启动线程数量:" + threadC + ",当前数据量：" + countRow);
    		for (int i = 0; i < threadC; i++) {
    			int beginR = i == 0 ? 1 : i * maxUnit + 1;
				int endR = i == (threadC -1 ) ? countRow : beginR + maxUnit - 1 ;
				ZyUpCmThread upCmThread = new ZyUpCmThread(beginR, endR, sheet);
				upCmThread.start();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
